import feedparser
import datetime
from google.appengine.api import memcache
from google.appengine.api.labs import taskqueue
from google.appengine.ext import db
from v2ex.babel import Member
from v2ex.babel import Counter
from v2ex.babel import Section
from v2ex.babel import Node
from v2ex.babel import Topic
from v2ex.babel import Reply

urls = ["http://xiao.heroku.com/index.xml",
        "http://ticktick.blog.51cto.com/rss.php?uid=823160",
        ]


d = feedparser.parse("http://xiao.heroku.com/index.xml")



q = db.GqlQuery("SELECT * FROM Node WHERE name = :1", 'feed')
node = False
if (q.count() == 1):
     node = q[0]


topic = Topic(parent=node)
q = db.GqlQuery('SELECT * FROM Counter WHERE name = :1', 'topic.max')
if (q.count() == 1):
     counter = q[0]
     counter.value = counter.value + 1
else:
     counter = Counter()
     counter.name = 'topic.max'
     counter.value = 1
q2 = db.GqlQuery('SELECT * FROM Counter WHERE name = :1', 'topic.total')
if (q2.count() == 1):
     counter2 = q2[0]
     counter2.value = counter2.value + 1
else:
     counter2 = Counter()
     counter2.name = 'topic.total'
     counter2.value = 1
topic.num = counter.value
topic.title = d['entries'][0]['title']
topic.content = d['entries'][0]['summary']
topic.node = node
topic.node_num = node.num
topic.node_name = node.name
topic.node_title = node.title
topic.created_by = d['entries'][0].author_detail.name
#topic.member = member
#topic.member_num = member.num
topic.last_touched = datetime.datetime.now()
topic.source = 'FeedParser'
node.topics = node.topics + 1
node.put()
topic.put()
counter.put()
counter2.put()
memcache.delete('feed_index')
memcache.delete('Node_' + str(topic.node_num))
memcache.delete('Node::' + str(node.name))
memcache.delete('home_rendered')
memcache.delete('home_rendered_mobile')
taskqueue.add(url='/index/topic/' + str(topic.num))



